#include <gdk/gdkkeysyms.h>
#include <string.h>
#include "sms.h"

#define TEACHER_PATH    "/usr/share/sms/default/layout/layout-teacher/"
#define TEACHER_LAYOUT_NAME "layout-teacher"
static int teacher_focus_id=0;



typedef struct _Icon
{
  char *name;

  GooCanvasItem *icon;
  GooCanvasItem *icon_a;

  LAYOUT_WIN_ID id;

  char *cmd;
  char **argv;
  char *layout_xml_path;

  int argc;

  int x;
  int y;
  int size;
  int asize;
}Icon;

typedef struct _Title{	
  int x;
  int y;
  int size;
  char *font;
  char *color;
  GooCanvasItem *text;
}Title;

typedef struct _Teacher_Layout
{
  GooCanvasItem *bg;
  int micon_count;
  int focus_id ;
  Icon icons[3];
  Exec *exec;
  Title title[3];

  Animate *animates;
  int animate_count;

  Layout *layout;
  char buf[512];
  //ListView *list;
  //GooCanvasItem *test;
}Teacher_Layout;


static gboolean teacher_layout_load_bg(xmlNode *a_node,Layout *layout)
{
 Teacher_Layout  *teachlayout = layout->private ;
 GooCanvasItem *ship ,*cloud ,*logo;
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  if(teachlayout->bg||!a_node)
    return FALSE;
	//background
  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,800,480);
  if(!pixbuf)
    return FALSE;

  teachlayout->bg=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);   
  g_object_unref(pixbuf);
	//clouds
  val = xmlGetProp (a_node,(xmlChar*)"clouds");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,-1,-1);
  if(!pixbuf)
    return FALSE;

  cloud = goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);   
  goo_canvas_item_set_simple_transform(cloud ,84 ,40 ,1 ,0);
  g_object_unref(pixbuf);
  
	//ships
  val = xmlGetProp (a_node,(xmlChar*)"ship");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,-1,-1);
  if(!pixbuf)
    return FALSE;

  ship = goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);   
  goo_canvas_item_set_simple_transform(ship ,360 ,-20 ,1 ,0);
  g_object_unref(pixbuf);

	//logo
  val = xmlGetProp (a_node,(xmlChar*)"logo");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,-1,-1);
  if(!pixbuf)
    return FALSE;

  logo = goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);   
  goo_canvas_item_set_simple_transform(logo ,90 ,40 ,1 ,0);
  g_object_unref(pixbuf);

  return TRUE;
}

#define ANIMATE_FREEZE   1
#define ANIMATE_RESET    2
#define ANIMATE_RESTART  3
#define ANIMATE_BOUNCE   4

static Animate * teacher_layout_create_animate(xmlNode *a_node,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  Teacher_Layout  *teachlayout = layout->private ;
  Animate *animate;
  xmlChar * val;
  char pic[256];

  gdouble start_x;
  gdouble start_y;
  gdouble stop_x;
  gdouble stop_y;

  gdouble init_scale;
  gdouble init_rotation;
  gdouble animate_scale;
  gdouble animate_rotation;
  gboolean isAbsolute;
  gint total_time;
  gint step_time;
  GooCanvasAnimateType type;

  start_x = sms_get_xml_double (a_node, (xmlChar*)"start_x", 0);
  start_y = sms_get_xml_double (a_node, (xmlChar*)"start_y", 0);
  stop_x = sms_get_xml_double (a_node, (xmlChar*)"stop_x", 0);
  stop_y = sms_get_xml_double (a_node, (xmlChar*)"stop_y", 0);
  init_scale = sms_get_xml_double (a_node, (xmlChar*)"init_scale", 0);
  init_rotation = sms_get_xml_double (a_node, (xmlChar*)"init_rotation", 0);
  animate_scale = sms_get_xml_double (a_node, (xmlChar*)"animate_scale", 0);
  animate_rotation = sms_get_xml_double (a_node, (xmlChar*)"animate_rotation", 0);
  isAbsolute = sms_get_xml_boolean (a_node, (xmlChar*)"isAbsolute");
  total_time = sms_get_xml_int (a_node, (xmlChar*)"total_time", 0);
  step_time = sms_get_xml_int (a_node, (xmlChar*)"step_time", 0);

  switch (sms_get_xml_int (a_node, (xmlChar*)"type", 1))
  {
    case ANIMATE_FREEZE:
      type = GOO_CANVAS_ANIMATE_FREEZE;
      break;
    case ANIMATE_RESET:
      type = GOO_CANVAS_ANIMATE_RESET;
      break;
    case ANIMATE_RESTART:
      type = GOO_CANVAS_ANIMATE_RESTART;
      break;
    case ANIMATE_BOUNCE:
      type = GOO_CANVAS_ANIMATE_BOUNCE;
      break;
    default: break;
  }
  g_print ("startx: %f, starty: %f, stopx: %f, stopy: %f,init_scale: %f, animate_scale: %f, animate_rotation: %f, total_time: %d, step_time %d\n",
      start_x, start_y, stop_x, stop_y, init_scale, animate_scale, total_time, step_time);

  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
    goto err;
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  g_print ("pic path: %s", pic);

  animate = animate_new (layout->cavas, pic, start_x, start_y, stop_x, stop_y, init_scale, init_rotation,
      animate_scale, animate_rotation, isAbsolute, total_time, step_time, type);
  //g_signal_connect (G_OBJECT (animate->cavas), "animation-finished", G_CALLBACK (setting_animation_finished), setlayout);
  return animate;
err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  return NULL;
}


static gboolean teacher_layout_load_animates(xmlNode *a_node, Layout *layout)
{
  Teacher_Layout  *teachlayout = layout->private ;

  xmlNode *cur_node = NULL;
  Animate * animate;

  if(!a_node)
    return FALSE;
  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE) {
      if(strcmp(cur_node->name,"animate")==0)
      {
        animate = teacher_layout_create_animate(cur_node, layout);
        if(animate)
        {
          teachlayout->animate_count++;
          if(teachlayout->animates)
          {
            teachlayout->animates->next = animate;
          }
          else
          {
            teachlayout->animates = animate;
          }
        }
      }
    }
  }
  return TRUE;
}


static void  teacher_layout_create_icon(xmlNode *a_node,Layout *layout ,int id)
{
  Teacher_Layout  *teachlayout = layout->private ;
  SMS *sms=layout->sms;
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  //Icon *icon = (Icon *)g_malloc0((Icon *)+1);
  

  printf("id=%d\n" ,id);
  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
  {
    goto err;
  }
  teachlayout->icons[id].name=g_strdup(val);
  teachlayout->icons[id].size=sms_get_xml_int(a_node,(xmlChar*)"size",64);
  teachlayout->icons[id].asize=sms_get_xml_int(a_node,(xmlChar*)"asize",teachlayout->icons[id].size);     
  teachlayout->icons[id].x=sms_get_xml_int(a_node,(xmlChar*)"x",teachlayout->icons[id].x);     
  teachlayout->icons[id].y=sms_get_xml_int(a_node,(xmlChar*)"y",teachlayout->icons[id].y);   

  /************icon creat****************/
  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
  {
    goto err;
  }

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,teachlayout->icons[id].size,teachlayout->icons[id].size);
  if(!pixbuf)
  {
    goto err;
  }

  teachlayout->icons[id].icon=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0 ,NULL);	
  g_object_set(teachlayout->icons[id].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  /*************icon ani******************/
  val = xmlGetProp (a_node,(xmlChar*)"apic");
  if(!val)
  {
    goto err;
  }

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,teachlayout->icons[id].asize,teachlayout->icons[id].asize);
  if(!pixbuf)
  {
    goto err;
  }

  //is layout ?
  val = xmlGetProp (a_node,(xmlChar*)"layout");
  if(val)
  {
	  char *xml_path = NULL;
	  int layout_id;

  	  xml_path = xmlGetProp (a_node,(xmlChar*)"layout_xml_path");
  	  printf("layout-teacher: xml_path = %s\n", xml_path);

  	  layout_id = sms_get_win_id(sms, val);
      if(id<0)
  		  goto err;
      teachlayout->icons[id].id = layout_id;
  	  if(xml_path)
  	  {
  		  teachlayout->icons[id].layout_xml_path = xml_path;
  	  }
  }

  teachlayout->icons[id].icon_a=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);
  g_object_set(teachlayout->icons[id].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  return ;

err:
  printf("fdfdfdf\n");
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  //return ;
}

static gboolean teacher_layout_load_icons(xmlNode *a_node,Layout *layout)
{
  GdkPixbuf*  pixbuf;
  char pic[256];

  Teacher_Layout  *teachlayout = layout->private ;
  xmlNode *cur_node = NULL;
  int i=0;
  Icon * icon;
  if(!a_node)
    return FALSE;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      if(strcmp(cur_node->name,"icon")==0){
        teacher_layout_create_icon(cur_node,layout ,i);
        i++;
      }
    }
  }
  teachlayout->micon_count = i ;
  return TRUE ;
}

static gboolean teacher_layout_load_title(xmlNode *a_node,Layout *layout)
{
  Teacher_Layout  *teachlayout = layout->private ;
  GooCanvasItem *text3;
  GooCanvasItem *text4;
  xmlNode *cur_node = a_node;
  xmlChar * val;
  char font[64];        
  char pic[256];
  int i = 0 ;

  if(!a_node)
    goto err;

  GooCanvasItem *logo;
  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" ,layout->name ,"logo.png");
  GdkPixbuf  *pixbuf_logo = gdk_pixbuf_new_from_file_at_size(pic ,120 ,138 ,NULL);
  logo = goo_canvas_image_new(layout->cavas ,pixbuf_logo ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(logo ,656 ,19 ,1 ,0);

	for(i ; i<teachlayout->micon_count ; i++)
	{
  	teachlayout->title[i].x=sms_get_xml_int(cur_node,(xmlChar*)"x",0);
  	teachlayout->title[i].y=sms_get_xml_int(cur_node,(xmlChar*)"y",0);
  	teachlayout->title[i].size=sms_get_xml_int(cur_node,(xmlChar*)"size",10);
	

  val = xmlGetProp (cur_node,(xmlChar*)"color");
  if(val){
    teachlayout->title[i].color=g_strdup(val);
  }
  val = xmlGetProp (cur_node,(xmlChar*)"font");
  if(val){
    teachlayout->title[i].font=g_strdup(val);
  }

  sprintf(font ,"%s %d",teachlayout->title[i].font ,teachlayout->title[i].size);
  teachlayout->title[i].text=goo_canvas_text_new(layout->cavas,"",
      teachlayout->title[i].x,teachlayout->title[i].y,-1,GTK_ANCHOR_CENTER,
      "font", font,NULL);

  if(teachlayout->title[i].color)
  {
    g_object_set(teachlayout->title[i].text,"fill_color",teachlayout->title[i].color,NULL);
  }
}
  text3 = goo_canvas_text_new(layout->cavas,"名师视\n频教学",
      180,83,-1,GTK_ANCHOR_CENTER,
      "font", "Sans Bold black",NULL);


  return TRUE;
err:
  return FALSE;
}

static gboolean teacher_layout_travel_xml(xmlNode *a_node,Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  Teacher_Layout  *teachlayout = layout->private ;
  xmlNode *cur_node = NULL;
  xmlNode *bg_node = NULL;
  xmlNode *icon_node = NULL;
  xmlNode *title_node = NULL;
  xmlNode *animate_node = NULL;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      printf("node type: Element, name: %s\n", cur_node->name);
      if(strcmp(cur_node->name,"bg")==0)
        bg_node=cur_node;
      else if(strcmp(cur_node->name,"icons")==0)
        icon_node=cur_node;
      else if(strcmp(cur_node->name,"title")==0)
        title_node=cur_node;
      else if(strcmp(cur_node->name,"animates")==0)
        animate_node = cur_node;
    }
  }

  if(!bg_node||!icon_node||!title_node||!animate_node)
    return FALSE;

  teacher_layout_load_bg(bg_node,layout);
  teacher_layout_load_animates (animate_node->children, layout);
  teacher_layout_load_icons(icon_node->children,layout);
  teacher_layout_load_title(title_node,layout);

  if(teachlayout->micon_count>=3)
    return TRUE;

  return FALSE;
}


gboolean teacher_layout_load_config(Layout *layout)
{
  gboolean ret=FALSE;
  xmlDoc *doc = NULL;
  xmlNode *root_element = NULL;
  Teacher_Layout  *teachlayout = layout->private ;
  char conf[256];
  sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name);
  printf("layout->name=%s\n" ,layout->name);

  doc = xmlReadFile(conf, NULL, 0);
  if (doc == NULL) {
    fprintf(stderr,"error: could not parse file %s\n", conf);
    return ret;
  }
  root_element = xmlDocGetRootElement(doc);

  if(root_element)
  {
    xmlNode *cur_node = NULL;
    for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
      if (cur_node->type == XML_ELEMENT_NODE) {
        ret=TRUE;
        break;
      }
    }
    if(ret)
      ret=teacher_layout_travel_xml(cur_node->children,layout);
  }

  xmlFreeDoc(doc);
  xmlCleanupParser();
  return ret;

}

void teacher_layout_reset_icons(Layout *layout)
{
  Teacher_Layout  *teachlayout = layout->private ;
  Icon * icon=teachlayout->icons;
  //test_layout->focus_id=0;
  int i ;
  
  for (i = 0 ; i < teachlayout->micon_count ; i++)
  {
    g_object_set(teachlayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
    g_object_set(teachlayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    goo_canvas_item_translate(teachlayout->icons[i].icon,
        teachlayout->icons[i].x,teachlayout->icons[i].y);    
    goo_canvas_item_translate(teachlayout->icons[i].icon_a,
        teachlayout->icons[i].x,teachlayout->icons[i].y);    
  }
  g_object_set(teachlayout->icons[1].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  g_object_set(teachlayout->icons[1].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,
  																						"x" , "350", "y" , "200",NULL);
  g_object_set(teachlayout->title[1].text ,"text" ,teachlayout->icons[1].name ,NULL);
}

static void teacher_change_icon(Layout *layout, int icon_id)
{
  Teacher_Layout  *teachlayout = layout->private;
  SMS *sms = layout->sms ;
	int i =0 ;
  
  teachlayout->focus_id = icon_id ;
  printf("micon_count = %d ,icon_id=%d\n" ,teachlayout->micon_count ,icon_id);

  for (i  ; i < teachlayout->micon_count ; i++)
  {
    if(i == icon_id)
    {
      printf("i= %d ,icon_id=%d\n" ,i ,icon_id);
      g_object_set(teachlayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      g_object_set(teachlayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
      g_object_set(teachlayout->title[i].text ,"text" ,teachlayout->icons[i].name ,NULL);
      printf("name = %s\n" ,teachlayout->icons[i].name);
    }
    else if(i != icon_id)
    {
      printf("!~i= %d ,icon_id=%d\n" ,i ,icon_id);
      g_object_set(teachlayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
      g_object_set(teachlayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      g_object_set(teachlayout->title[i].text ,"text" ,"" ,NULL);
      printf("name = %s\n" ,teachlayout->icons[i].name);
    }
  }
}


static void teacher_layout_move_icon(Layout *layout,gboolean is_up)
{
  Teacher_Layout  *teachlayout = layout->private;
  SMS *sms = layout->sms ;

  if(is_up)
  {
    teacher_change_icon(layout ,abs((teacher_focus_id)++)%(teachlayout->micon_count));
    sms->busy = FALSE ;
  }
  else
  {
    teacher_change_icon(layout ,abs((teacher_focus_id)--)%(teachlayout->micon_count));
    sms->busy = FALSE ;
  }
}


static gboolean teacher_layout_key_press(Layout * layout,GdkEventKey *event)
{
  Teacher_Layout  *teachlayout ;
  SMS *sms = layout->sms;
  int i;
  teachlayout = layout->private;

  if(event->keyval==GDK_Right){
    teacher_layout_move_icon(layout ,TRUE);
    return TRUE;
  }
  else if(event->keyval==GDK_Left){
    teacher_layout_move_icon(layout ,FALSE);
    return TRUE;
  }
  else if(event->keyval == GDK_Escape)
  {		
    sms_change_win_to(layout->sms,LAYOUT_WIN_MAIN);
    return TRUE;
  }
  else if(event->keyval == GDK_Return )
  {
    printf("cur_icon_id = %d\n" ,teachlayout->focus_id);
    if(teachlayout->focus_id == 1)
    {
    		printf("1kemu\n");
    		sms_change_win_to(layout->sms,LAYOUT_WIN_TEACHER_SUBJECT);
    }
    else if (teachlayout->focus_id == 0)
    {
    		printf("2paihang\n");
    		int icon_id = teachlayout->focus_id;
      sms_change_win_with_property(layout->sms, LAYOUT_WIN_TOP_LIST, "xml_path" ,teachlayout->icons[icon_id].layout_xml_path);
    }
    else if(teachlayout->focus_id == 2)
    {
    		printf("3mingshi\n");
    		int icon_id = teachlayout->focus_id;
    		sms_change_win_with_property(layout->sms, LAYOUT_WIN_VIDEO_LIST, "xml_path", teachlayout->icons[icon_id].layout_xml_path);
    }
  	}
}


static gboolean teacher_layout_clear(Layout *layout,gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	Teacher_Layout  *teachlayout = layout->private ;

	g_object_unref(teachlayout->bg);
	

	g_free(teachlayout);
	return TRUE ;
}

static gboolean teacher_layout_init(Layout *layout,gpointer data)
{
	printf("~~~%s\n" ,__FUNCTION__);
  Teacher_Layout  *teachlayout = (Teacher_Layout *)g_malloc0(sizeof (Teacher_Layout)+1);
  //lanlayout->focus_id = -1 ;
  layout->private = (void *)teachlayout ;
  layout->cavas = goo_canvas_group_new(layout->parent,NULL);	
  teachlayout->focus_id = 1 ;
  
  if(!teacher_layout_load_config(layout))
  {
    fprintf(stderr,"%s:load config err\n",__FUNCTION__);
    //return FALSE;//exit(-1);
  }
  teacher_layout_reset_icons(layout);

  return TRUE;
}


Layout *teacher_layout_new(GooCanvasItem * parent,gpointer sms)
{
  Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN ,LAYOUT_WIN_TEACHER,
      TEACHER_LAYOUT_NAME,sms);

  layout_set_func(layout,"init",teacher_layout_init);
  layout_set_func(layout,"clear",teacher_layout_clear);
  layout_set_func(layout,"key-press",teacher_layout_key_press);

  return layout;
}
